#include "dot.h"
#include "constant.h"
#include "globals.h"

Dot::Dot()
{
    //Initialize the offsets
    box.x = SCREEN_WIDTH / 2;
    box.y = SCREEN_HEIGHT / 2;

    box.w = DOT_WIDTH;
    box.h = DOT_HEIGHT;

    //Initialize the velocity
    xVel = 0;
    yVel = 0;
}

void Dot::reset()
{
    //Initialize the offsets
    box.x = SCREEN_WIDTH / 2;
    box.y = SCREEN_HEIGHT / 2;

    box.w = DOT_WIDTH;
    box.h = DOT_HEIGHT;

    //Initialize the velocity
    xVel = 0;
    yVel = 0;
}

void Dot::handle_input()
{
    //If a key was pressed
    if( event.type == SDL_KEYDOWN )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel -= DOT_HEIGHT / SPEED_FACTOR; break;
            case SDLK_DOWN: yVel += DOT_HEIGHT / SPEED_FACTOR; break;
            case SDLK_LEFT: xVel -= DOT_WIDTH / SPEED_FACTOR; break;
            case SDLK_RIGHT: xVel += DOT_WIDTH / SPEED_FACTOR; break;
        }
    }
    //If a key was released
    else if( event.type == SDL_KEYUP )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel += DOT_HEIGHT / SPEED_FACTOR; break;
            case SDLK_DOWN: yVel -= DOT_HEIGHT / SPEED_FACTOR; break;
            case SDLK_LEFT: xVel += DOT_WIDTH / SPEED_FACTOR; break;
            case SDLK_RIGHT: xVel -= DOT_WIDTH / SPEED_FACTOR; break;
        }
    }
}

void Dot::move()
{
    //Move the dot left or right
    box.x += xVel;

    //If the dot went too far to the left or right
    if( ( box.x < 0 ) || ( box.x + DOT_WIDTH > SCREEN_WIDTH ) )
    {
        //move back
        box.x -= xVel;
    }

    //Move the dot up or down
    box.y += yVel;

    //If the dot went too far up or down
    if( ( box.y < 0 ) || ( box.y + DOT_HEIGHT > SCREEN_HEIGHT ) )
    {
        //move back
        box.y -= yVel;
    }
}

void Dot::show()
{
    //Show the dot
    apply_surface( box.x, box.y, dot, screen );
}

SDL_Rect Dot::getBox()
{
    return box;
}
